package com.macro.mall.demo.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.domain.EsDocDto;
import com.macro.mall.demo.dto.RequesSelectEs;
import com.macro.mall.demo.service.EsDocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author：贾维斯
 * @Description: 描述
 * @name：MatchQueryInfo
 * @Date：2024/10/22 9:42
 */
@RestController
@RequestMapping("/doc")
@Validated
@Api(tags = "es文档管理")
public class EsDocController {
    @Autowired
    private EsDocService esDocService;

    /**
     * 批量添加
     * @param esDocDto 文档信息
     * @return 添加结果
     */
    @ApiOperation(value = "批量添加")
    @PostMapping("/batchSave")
    public CommonResult batchSave(@Validated @RequestBody EsDocDto esDocDto) {
        esDocService.batchAdd(esDocDto);
        return CommonResult.success("批量添加成功");
    }

    /**
     * 批量删除
     * @param indexName 索引名称
     * @param ids 多个id
     * @return 删除结果
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public CommonResult batchDelete(@RequestParam("indexName") @NotEmpty(message = "索引名称不能为空") String indexName,
                                    @RequestParam("docIds") @NotBlank(message = "文档id不能为空") @Size(min = 1, message = "文档id列表至少包含一个有效的id") List<String> ids) {
        esDocService.batchDelete(indexName,ids);
        return CommonResult.success("批量删除成功");
    }

    /**
     * 查询
     *
     * @param requesSelectEs
     * @return
     */
    @ApiOperation(value = "查询")
    @PostMapping("/selectEs")
    public CommonResult<List<Object>> selectEs(@Validated @RequestBody RequesSelectEs requesSelectEs) {
       List<Object> productList = esDocService.selectEs(requesSelectEs);
        return CommonResult.success(productList);
    }

    /**
     * 查询
     * @param requesSelectEs
     * @return
     */
    @PostMapping("/matchQueryInfo")
    public List<Object> matchQueryInfo(@Validated @RequestBody RequesSelectEs requesSelectEs) {
        List<Object> matchQueryInfo = esDocService.selectMatch(requesSelectEs);
        return matchQueryInfo;
    }
}
